<?php

class Silk_Mapi_Model_Api2_Shipping extends Mage_Api2_Model_Resource
{

    protected $_http_client;

    /**
     * @return Silk_Tms_Model_Mysql4_Shipping_Collection
     * @throws Exception
     */
    protected function getShippingCollection()
    {
        $shippingCollection = Mage::getModel('tms/shipping')->getCollection()
            ->addFieldToFilter('website_id', array('eq' => $this->getCurrentUser()->getWebsiteId()))
            ->addFieldToFilter('user_id', array('eq' => $this->getApiUser()->getUserId()))
            ->addFieldToFilter('status', array('neq' => Silk_Tms_Helper_Status::STATUS_SHIPPING_CANCELED))
            ->setOrder("status='delivered'", 'ASC')
            ->setOrder('id', 'DESC');
        
        return $shippingCollection;
    }
    
    protected function getCurrentUser()
    {
        $adminUser = Mage::getModel('admin/user')->load($this->getApiUser()->getUserId());
        if ($adminUser && $adminUser->getId()) {
            return $adminUser;
        }
        $this->_critical('登录已过期！', Mage_Api2_Model_Resource::ERROR_NOT_LOGIN);
    }


    protected function formatDateTime($time)
    {
        date_default_timezone_set('Asia/Shanghai');
        
        if (is_string($time)) {
            $time = strtotime($time);
        }

        return date('Y-m-d H:i:s', $time);
    }
    
    protected function formatPrice($price)
    {
        return (float)$price;
    }

    protected function addEightHours($time)
    {
        $time = strtotime($time) + 3600 * 8;

        return date('Y-m-d H:i:s', $time);
    }

    protected function getStoreInfo($customerId)
    {
        $results = array();
        /** @var Credis_Client $_redis */
        $_redis = Mage::helper('qslb')->getRedis();

        $_redis->connect();

        if ($_redis->isConnected()) {
            $hMKey = 'store:' . $customerId;
            if ($_redis->exists($hMKey)) {
                $results = $_redis->hGetAll($hMKey);
            }
        }
        if ($results) return $results;

        if($results) return $results;

        $results = $this->getStoreInfoFromYii($customerId);

        return $results;
    }

    /**
     * return store information and save it to redis
     * @param $customerId
     * @return array
     * @throws Exception
     * @throws Mage_Api2_Exception
     * @throws Zend_Http_Client_Exception
     */
    protected function getStoreInfoFromYii($customerId)
    {
        $path = trim(Mage::getStoreConfig('general/api/customer_push_url'), '/') . '/shops/' . $customerId;
        $response = $this->_http()
            ->setUri($path)
            ->request(Zend_Http_Client::GET);

        if ($response->getStatus() == 200) {
            $data = json_decode($response->getBody());
            if ($data->code == 200) {
                return (array)$data->data;
            }
            $this->_critical($data->message, Mage_Api2_Model_Resource::ERROR_UNKNOWN_ERROR);
        }
        $this->_critical('获取店铺信息失败！', Mage_Api2_Model_Resource::ERROR_UNKNOWN_ERROR);
    }

    /**
     * @return Zend_Http_Client
     */
    protected function _http()
    {

        if (!$this->_http_client) {
            $this->_http_client = new Zend_Http_Client;
        }

        return $this->_http_client;

    }

    public function convertDate($time)
    {
        if (is_string($time)) {
            $time = strtotime($time);
        }

        date_default_timezone_set('Asia/Shanghai');

        return date('Y-m-d H:i:s', $time);
    }
}